package com.open.loan.controller;

import java.text.DecimalFormat;	
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.open.common.controller.BaseController;
import com.open.loan.domain.rsp.RspFaceLogInfo;
import com.open.loan.utils.ResponseUtil;
import com.open.system.domain.LoanShopDO;
import com.open.system.domain.rsp.RspAdminCountModel;
import com.open.system.domain.rsp.RspAdminShopInfoModel;
import com.open.system.service.LoanAdminLogService;
import com.open.system.service.LoanFaceLogService;
import com.open.system.service.LoanShopService;
import com.open.system.service.LoanUserService;

@Controller
@RequestMapping("/moblie")
public class AdminController extends BaseController {
	
	
	@Autowired
	private LoanUserService userService;
	
	@Autowired
	private LoanShopService shopService;
	
	@Autowired
	private LoanFaceLogService loanFaceLogService;
	
	@Autowired
	private LoanAdminLogService LoanAdminLogService;
	
	@RequestMapping(value ="/getAdminData",method = RequestMethod.GET)
	public RspAdminCountModel getAdminData(HttpServletRequest request,HttpServletResponse response){
		DecimalFormat count1=new DecimalFormat(",###,##0"); //没有小数  
		DecimalFormat count2=new DecimalFormat(",###,##0.00"); //保留一位小数  
		//店铺ID
		String shopId=null;
		String shijiShopId=null;
		if(getUser()!=null){
			shopId=getUser().getEmail();
			shijiShopId=shopId;
		}
		if("1".equals(shopId)){
			shopId=null;
		}
		Map<String,String>shopIdMap=new HashMap<String,String>();
		shopIdMap.put("shopId",shopId);
		RspAdminCountModel model=new RspAdminCountModel();
		Long totalUser=userService.totalUser(shopIdMap);
		Long bankUser=userService.bankUser(shopIdMap);
		Long applyLoanUser=userService.applyLoanUser(shopIdMap);
		model.setTotalUser(count1.format(totalUser));
		model.setBankUser(count1.format(bankUser));
		model.setApplyLoanUser(count1.format(applyLoanUser));
		
		String today=resultTime(0);
		Map<String,String>map=new HashMap<String,String>();
		map.put("today",today+" 00:00:01");
		map.put("shopId",shopId);
		Long totayRegUser=userService.totayRegUser(map);
		Long todayBankUser=userService.todayBankUser(map);
		Long todayApplyLoanUser=userService.todayApplyLoanUser(map);
		Long totayLoanUser=userService.totayLoanUser(map);
		model.setTotayRegUser(count1.format(totayRegUser));
		model.setTodayBankUser(count1.format(todayBankUser));
		model.setTodayApplyLoanUser(count1.format(todayApplyLoanUser));
		model.setTotayLoanUser(count1.format(totayLoanUser));
		
		
		Long applyAmt=userService.applyAmt(shopIdMap);
		Long loanAmt=userService.loanAmt(shopIdMap);
		if(applyAmt==null){
			applyAmt=0L;
		}
		if(loanAmt==null){
			loanAmt=0L;
		}
		
		model.setApplyAmt(count2.format(applyAmt));
		model.setLoanAmt(count2.format(loanAmt));
		
		
		Long alreadyRepayAmt=userService.alreadyRepayAmt(shopIdMap);
		Long waitRepayAmt=userService.waitRepayAmt(shopIdMap);
		if(alreadyRepayAmt==null){
			alreadyRepayAmt=0L;
		}
		if(waitRepayAmt==null){
			waitRepayAmt=0L;
		}
		Long juxinliMig=userService.juxinliMig(shopIdMap);
		Long juxinliMif=userService.juxinliMif(shopIdMap);
		model.setAlreadyRepayAmt(count2.format(alreadyRepayAmt));
		model.setWaitRepayAmt(count2.format(waitRepayAmt));
		model.setJuxinliMig(count1.format(juxinliMig));
		model.setJuxinliMif(count1.format(juxinliMif));
		
		Long productCount=userService.productCount(shopIdMap);
		Long shopCount=userService.shopCount(shopIdMap);
		model.setProductCount(count1.format(productCount));
		model.setShopCount(count1.format(shopCount));
		
		
		//已注册和剩余
		LoanShopDO loanShopDO=shopService.get(Long.parseLong(shijiShopId));
		if(loanShopDO!=null){
			if(StringUtils.isNotEmpty(loanShopDO.getRemark4())){
				Integer str=Integer.parseInt(loanShopDO.getRemark4());
				int remark4=str.intValue();
				Map<String,String>shijiMap=new HashMap<String,String>();
				shijiMap.put("shopId",shijiShopId);
				Long shijiUserTotal=userService.totalUser(shijiMap);
				if(shijiUserTotal==null){
					shijiUserTotal=0L;
				}
				model.setHasRegeterNum(String.valueOf(shijiUserTotal));
				int shijiTotal=shijiUserTotal.intValue();
				if(remark4>shijiTotal){
					int a=remark4-shijiTotal;
					model.setNoRegeterNum(String.valueOf(a));
				}else {
					model.setNoRegeterNum("0");
				}
			}
		}else {
			model.setHasRegeterNum("0");
			model.setNoRegeterNum("0");
		}
		
		ResponseUtil.toWrite("200","获取信息成功",model, response);
		return null;
	}
	
	//获取年月日
	public String resultTime(int num){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");  
		Calendar  c  =  Calendar.getInstance();   
		c.add(Calendar.DAY_OF_MONTH, num);  
		String mDateTime=formatter.format(c.getTime());  
		String resultTime=mDateTime.substring(0, 10);
		return resultTime;
	}
	
	
	@RequestMapping(value ="/getAdminShopInfo",method = RequestMethod.GET)
	public RspAdminShopInfoModel getAdminShopInfo(HttpServletRequest request,HttpServletResponse response){
		RspAdminShopInfoModel model=new RspAdminShopInfoModel();
		String shopId=getUser().getEmail();
		String shopName="后台管理账户";
		String requestCode="4711";
		//String shopAdrress="后台管理账户";
		if(shopId!=null && shopId!=""){
			LoanShopDO shop=shopService.get(Long.parseLong(shopId));
			if(shop!=null){
				shopName=shop.getShopName();
				requestCode=shop.getRemark2();
			}
		}
		model.setShopName(shopName);
		model.setRequestCode(requestCode);
		ResponseUtil.toWrite("200","获取信息成功",model, response);
		return null;
	}
	
	@RequestMapping(value ="/getFaceLogInfo",method = RequestMethod.GET)
	public RspFaceLogInfo getFaceLogInfo(HttpServletRequest request,HttpServletResponse response){
		RspFaceLogInfo model=new RspFaceLogInfo();
		String shopId=getUser().getEmail();
		Map<String,Object>map=new HashMap<String,Object>();
		if(!"1".equals(shopId)){
			map.put("remark3",shopId);
		}
		int total=loanFaceLogService.count(map);
		model.setTotal(String.valueOf(total));
		model.setShopId(shopId);
		ResponseUtil.toWrite("200","获取信息成功",model, response);
		return null;
	}
	
	
	@RequestMapping(value ="/getAdminLogInfo",method = RequestMethod.GET)
	public RspFaceLogInfo getAdminLogInfo(HttpServletRequest request,HttpServletResponse response){
		RspFaceLogInfo model=new RspFaceLogInfo();
		String shopId=getUser().getEmail();
		Map<String,Object>map=new HashMap<String,Object>();
		if(!"1".equals(shopId)){
			map.put("remark4",shopId);
		}
		int total=LoanAdminLogService.count(map);
		model.setTotal(String.valueOf(total));
		model.setShopId(shopId);
		ResponseUtil.toWrite("200","获取信息成功",model, response);
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println(5/6+1);
	}

}
